home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 6 / MacMania 6.toast / / Tools&Utilities / EnterAct Stuff / Drag_on Modules / hAWK programs / $Print_MPSR_1007 < prev    next >
Text File  |  1993-04-09  |  4KB  |  163 lines

  1. #$Print_MPSR_1007: print marker resource, as loaded by Read Resource.
  2.  
  3. #The approach below uses "input on demand" to allow us
  4. #to step through the bytes in the resource. Each call to
  5. #GetNextByte or PrintCharAndAdvance retrieves the next byte, grabbing
  6. #the next input line if necessary. The variable bytePos is
  7. #automatically advanced whenever a byte is read, and the calculation
  8. #of the field number and position within the field is hidden within
  9. #GetNextByte. To skip over n bytes, add n to bytePos.
  10.  
  11. #Format of mark resource:
  12. #2        numMarks
  13. #--repeated for numMarks:
  14. #4        start
  15. #4        end
  16. #1        n = oddLen; rounded up to be odd
  17. #n        mark text, padded sometimes with null
  18.  
  19. #Format of resource as retrieved by Read Resource:
  20. #byteNumber: 4bytes 4bytes 4bytes 4bytes  ascii version of the bytes
  21. #the last line may be padded with zeroes, for example
  22. #      240: 07556E6D 61726BC9 00000000 09417574  .Unmark......Aut
  23. #      256: 6F6D6172 6BC90000 00000000 00000000  omark......
  24.  
  25. #bytePos ranges 0 : up. It is automatically
  26. #advanced when a byte is read by GetNextByte(). It can be
  27. #manually advanced to skip unwanted bytes.
  28.  
  29. # User’s Manual references:
  30. # «hAWK User’s Manual» «F   Running hAWK programs»
  31. # «hAWK User’s Manual» «L  5   Regular expressions»
  32. # «hAWK User’s Manual» «M  5   Built-in string and file functions»
  33. # «hAWK User’s Manual» «K  4   Built-in variables»
  34. # «hAWK User’s Manual» «K  8   Arrays»
  35. # «hAWK User’s Manual» «N   User-defined functions»
  36. # «hAWK User’s Manual» «P  3   The getline function»
  37. # «hAWK User’s Manual» «O  3   Output into files»
  38. # «hAWK User’s Manual» «Q   The hAWK function»
  39.  
  40. FNR == 1    {
  41.             numMarks = GetNextBytesAsNumber(2)
  42.             print "Total marks:", numMarks;
  43.             while (1)
  44.                 {
  45.                 start = GetNextBytesAsNumber(4)
  46.                 end = GetNextBytesAsNumber(4)
  47.                 markLen = GetNextByte()
  48.                 printf("%6d %6d %4d %3d ", start, end, end-start, markLen);
  49.                 while (markLen > 0)
  50.                     {
  51.                     PrintCharAndAdvance();
  52.                     --markLen;
  53.                     }
  54.                 print ""
  55.                 }
  56.             }
  57.  
  58.  
  59.  
  60. function PrintCharAndAdvance()
  61.     {
  62.     printf("%c", GetNextByte());
  63.     }
  64.  
  65. #Convert ASCII representation of next byte, eg '1F', to a single
  66. #number, eg 31.
  67. function GetNextByte(        lineByte, field, char, pos)
  68.     {
  69.     #Convert byte position in resource to field and position in field.
  70.     lineByte = bytePos - (FNR - 1) * 16;
  71.     field = int((lineByte)/4);
  72.     while (field > 3)#the next line of input is wanted
  73.         {
  74.         if ((getline) <= 0)
  75.             {
  76.             print ""; print "End of file."; exit;
  77.             }
  78.         lineByte = bytePos - (FNR - 1) * 16;
  79.         field = int((lineByte)/4);
  80.         }
  81.     pos = lineByte - field * 4;
  82.     field += 2;
  83.     pos = 2 * pos + 1;
  84.     char = HexToDec(substr($field, pos, 2));
  85.     ++bytePos;
  86.     return char
  87.     }
  88.  
  89. #Return raw ASCII representation of byte, eg "2F".
  90. function GetNextRawByte(        lineByte, field, char, pos)
  91.     {
  92.     #Convert byte position in resource to field and position in field.
  93.     lineByte = bytePos - (FNR - 1) * 16;
  94.     field = int((lineByte)/4);
  95.     while (field > 3)#the next line of input is wanted
  96.         {
  97.         if ((getline) <= 0)
  98.             {
  99.             print ""; print "End of file."; exit;
  100.             }
  101.         lineByte = bytePos - (FNR - 1) * 16;
  102.         field = int((lineByte)/4);
  103.         }
  104.     pos = lineByte - field * 4;
  105.     field += 2;
  106.     pos = 2 * pos + 1;
  107.     ++bytePos;
  108.     return substr($field, pos, 2);
  109.     }
  110.  
  111. #Convert ASCII representation of 1, 2, or 4 bytes to a single
  112. #unsigned number.
  113. function GetNextBytesAsNumber(howManyBytes,        i, temp, numString, number)
  114.     {
  115.     for (i = 1; i <= howManyBytes; ++i)
  116.         {
  117.         temp = GetNextRawByte();
  118.         numString = numString temp;
  119.         }
  120.     number = HexToDec(numString);
  121.     return number;
  122.     }
  123.  
  124. #General purpose ASCII hex to number conversion.
  125. function HexToDec(h,     len, nyb, hexDigit, power, d)
  126.     {
  127.     d = 0;
  128.     len = length(h);
  129.     power = 1
  130.     for (nyb = len; nyb >= 1; --nyb)
  131.         {
  132.         hexDigit = substr(h, nyb, 1);
  133.         d += DecimalEquiv(hexDigit) * power;
  134.         power *= 16;
  135.         }
  136.     return d
  137.     }
  138.  
  139. #Convert one hex "digit" to the equivalent number.
  140. function DecimalEquiv(hd, dd)
  141.     {
  142.     dd = 0;
  143.     if (match(hd,/[0-9]/) > 0)
  144.         dd = hd + 0;#conversion from string to number is implicit
  145.     else if (match(hd, /[a-fA-F]/) > 0)
  146.         {
  147.         if (match(hd, /[aA]/) > 0)
  148.             dd = 10;
  149.         else if (match(hd, /[bB]/) > 0)
  150.             dd = 11;
  151.         else if (match(hd, /[cC]/) > 0)
  152.             dd = 12;
  153.         else if (match(hd, /[dD]/) > 0)
  154.             dd = 13;
  155.         else if (match(hd, /[eE]/) > 0)
  156.             dd = 14;
  157.         else if (match(hd, /[fF]/) > 0)
  158.             dd = 15;
  159.         }
  160.     return dd
  161.     }
  162.  
  163.